package lntu.cn.cxx1005;


import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment2 extends Fragment {

    private MainActivity activity;
    public Fragment2() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity =(MainActivity) getActivity();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_fragment2, container, false);
        //找到ListView
        ListView lvMain=view.findViewById(R.id.lv_main2);
        //创建适配器
        Fragment2.MyAdapter adapter=new Fragment2.MyAdapter();
        //为ListView添加适配器
        lvMain.setAdapter(adapter);
        return view;
    }

    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return 30;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView==null){
                convertView=View.inflate(activity.getApplicationContext(), R.layout.item2, null);
            }
            TextView tvTitle=convertView.findViewById(R.id.tv_titile2);
            tvTitle.setText("第"+(position+1)+"行");
            return convertView;
        }
    }
}
